package com.ibbi.admin.client.front;

import java.util.List;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.SplitLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import com.ibbi.admin.client.front.category.CategoryTree;
import com.ibbi.admin.client.front.category.CategoryTreeUtil;
import com.ibbi.admin.client.resource.IBBIResources;
import com.ibbi.admin.client.rpc.Services;
import com.ibbi.admin.client.rpc.obj.Category;
import com.ibbi.admin.client.session.ErrorHandler;
import com.ibbi.admin.client.session.SessionManager;

import corelib.client.delegate.DataCallback;
import corelib.client.ui.widget.page.PageBase;
import corelib.client.ui.widget.panel.SimpleLayoutPanel;
import corelib.client.ui.widget.toolbar.SimpleRibbon;
import corelib.client.ui.widget.tree.SimpleTreeNode;

public class CategoryManager extends PageBase<Widget> {
	
	private SimpleRibbon ribbon;
	private CategoryTree categoryTree;
	
	public CategoryManager(){
		setPageName("栏目信息管理");
		setPageIcon(IBBIResources.INSTANCE.category24());
	}
	
	@Override
	protected Widget createWidget() {
		SimpleLayoutPanel layout = new SimpleLayoutPanel();
		SplitLayoutPanel contentPanel  = createContentPanel();
		layout.add(ribbon = createRibbon());
		layout.add(contentPanel);
		layout.setWidgetTopHeight(ribbon, 0, Unit.PX, 80, Unit.PX);
		layout.setWidgetTopBottom(contentPanel, 80, Unit.PX, 0, Unit.PX);
		return layout;
	}
	
	protected SimpleRibbon createRibbon(){
		SimpleRibbon ribbon = new SimpleRibbon();
		return ribbon;
	}
	
	protected SplitLayoutPanel createContentPanel() {
		SplitLayoutPanel contentPanel = new SplitLayoutPanel();
		categoryTree = new CategoryTree(new DataCallback<Category>(){
			@Override
			public void onCompleted(Category data) {
				if (data != null) {
				}	
			}
		});
		contentPanel.addWest(categoryTree, 260);
		return contentPanel;
	}
	
	@Override
	protected void afterCreateWidget(Widget widgetCreated) {
		super.afterCreateWidget(widgetCreated);
		loadCategory();
	}

	private void loadCategory() {
		Services.CategoryService.ListAll(SessionManager.getPassport(), new AsyncCallback<List<Category>>(){
			@Override
			public void onFailure(Throwable caught) {
				ErrorHandler.handleServiceError(caught);
			}

			@Override
			public void onSuccess(List<Category> result) {
				SimpleTreeNode node = CategoryTreeUtil.convert(result);
				categoryTree.clear();
				categoryTree.getTree().addNode(node);
			}
		});
	}
}
